<?php

namespace IMT\EventBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use IMT\EventBundle\Entity\Comment;

class CommentController extends Controller
{
	public function loadCommentAction($id)
    {
    	$repository = $this->getDoctrine()->getRepository('IMTEventBundle:Event');
    	$eventObject = $repository->findOneById($id);
    	
    	$comments = $eventObject->getComments();
    	
    	return $this->render('IMTEventBundle:Event:comment.html.twig', array("comments" => $comments)); 	
    }
    
    public function addCommentAction($id)
    {
    	$request = $this->getRequest();
    	$comment = $request->request->get('comment');
    	
    	$commentObject = new Comment();
    	$commentObject->setContent($comment);
    	$commentObject->setDate(date_create(date("Y-m-d H:i:s")));
    	
    	$repository = $this->getDoctrine()->getRepository('IMTEventBundle:Event');
    	
    	$eventObject = $repository->findOneById($id);
    	
    	$commentObject->setEvent($eventObject);
    	
    	$em = $this->getDoctrine()->getEntityManager();
        $em->persist($commentObject);
        $em->persist($eventObject);

        $em->flush();
        
    	return $this->loadCommentAction($id); // TODO
    }
    
    public function deleteCommentAction()
    {
    	$request = $this->getRequest();
    	$repository = $this->getDoctrine()->getRepository('IMTEventBundle:Comment');
    	$commentObject = $repository->findOneById($request->request->get('id_comment'));
    	$em = $this->getDoctrine()->getEntityManager();
    	$em->remove($commentObject);
    	$em->flush();
    }
}


